package top.i51213.fastaction.png;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

public class Chunk {
    private int length;
    private String type;
    private byte[] data;
    private int CRC;

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public int getCRC() {
        return CRC;
    }

    public void setCRC(int CRC) {
        this.CRC = CRC;
    }

    private static void write(OutputStream out, byte[] buff) throws IOException {
        out.write(buff, 0, buff.length);
    }

    public void writeTo(OutputStream out) throws IOException {
        out.write(Util.int2Buff(getLength()));
        out.write(getType().getBytes(StandardCharsets.US_ASCII));
        out.write(getData(), 0, getLength());
        byte[] buff =Util.getCrc32(getType(),getData(),0,getLength());
        out.write(buff);
    }
}
